package org.xinjiang.data.migration.flush.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.xinjiang.data.migration.flush.service.ISettingQueryService;
import org.xinjiang.data.migration.xinjiang.enums.KeyIdEnum;
import org.xinjiang.data.migration.xinjiang.service.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.xinjiang.data.migration.xinjiang.enums.GzryConditionEnum.NOT;
import static org.xinjiang.data.migration.xinjiang.enums.GzryConditionEnum.NOT_PAGE;


/**
 * @author: 邢卫
 * @date: 2023年12月14日 18:07
 */
@Service
public class SettingQueryServiceImpl<T> implements ISettingQueryService<T> {

    @Autowired
    private WegovOrgTeamService sysDepartService;

    @Autowired
    private WegovOrgUserService sysUserService;

    @Autowired
    private WegovOrgMemberTeamService sysDepartParentService;

    @Autowired
    private SqjzGzryService gzryService;

    @Autowired
    private SqjzZyzglService zyzglService;

    @Override
    public IPage<T> queryPageList(KeyIdEnum idEnum, IPage<T> page) {
        return queryPageList(idEnum, page, null, null);
    }

    @Override
    public IPage<T> queryPageList(KeyIdEnum idEnum, IPage<T> page, List<String> ids) {
        return queryPageList(idEnum, page, ids, null);
    }

    private IService toService(KeyIdEnum idEnum) {
        switch (idEnum) {
            case TABLE_SYS_DEPART:
                return sysDepartService;
            case TABLE_SYS_USER:
                return sysUserService;
            case TABLE_SYS_DEPART_PARENT:
                return sysDepartParentService;
            case TABLE_MST_COMMUNITY_WORKER:
            case TABLE_MST_CORRECTIONTEAM_FUNCTIONARY:
                return gzryService;
            case TABLE_MST_COMMUNITY_VOLUNTEER:
                return zyzglService;
            default:
                throw new RuntimeException("当前接口还未实现");
        }
    }

    @Override
    public IPage<T> queryPageList(KeyIdEnum idEnum, IPage<T> page, List<String> ids, Map<String, Object> andCondition) {
        IService service = toService(idEnum);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!CollectionUtils.isEmpty(ids)) {
            String idName = idEnum.getTableIdName();
            queryWrapper.in(idName, ids);
        }
        if (!CollectionUtils.isEmpty(andCondition)) {
            if (andCondition.containsKey(NOT)) {
                Object remove = andCondition.get(NOT);
                if (remove instanceof Collection) {
                    Collection<?> value = (Collection<?>) remove;
                    if (!CollectionUtils.isEmpty(value)) {
                        Object[] objects = value.stream().toArray();
                        queryWrapper.notIn(idEnum.getTableIdName(), objects);
                    }
                }
            } else if (andCondition.containsKey(NOT_PAGE)) {
                Object remove = andCondition.get(NOT_PAGE);
                long current = page.getCurrent();
                if ((remove instanceof Integer) && current == 1) {
                    current = ((Integer) remove).intValue();
                }
                page.setCurrent(current);
            }

            andCondition.forEach((key, value) -> {
                if (!(NOT_PAGE.equals(key) || NOT.equals(key))) {
                    if (value instanceof Collection) {
                        Collection<?> collection = (Collection<?>) value;
                        if (!CollectionUtils.isEmpty(collection)) {
                            Object[] objects = collection.stream().toArray();
                            queryWrapper.in(key, objects);
                        }
                    } else if (value.getClass().isArray()) {
                        queryWrapper.in(key, (Object[]) value);
                    } else {
                        queryWrapper.eq(key, value);
                    }
                }
            });
        }
        return service.page(page, queryWrapper);
    }
}
